<?php
declare (strict_types = 1);

namespace app\listener;

use app\common\lib\AdminAuth;
use think\App;
use think\Http;

class HttpEnd
{
    /**
     * @var App
     */
    protected $app;
    /**
     * @var Http
     */
    protected $http;
    public function __construct(App $app,Http $http)
    {
        $this->app = $app;
        $this->http = $http;
    }
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event)
    {
        $start_time = $this->app->getBeginTime();
        $start_mem = $this->app->getBeginMem();
        $now_time = microtime(true);
        $now_mem = memory_get_usage();
        $use_time = $now_time - $start_time;
        $use_mem = $now_mem - $start_mem;
        $auth_map = [
            'admin'=>AdminAuth::class
        ];
        $module = $this->http->getName();
        if(!isset($auth_map[$module])){
            return true;
        }
        $auth_class = $auth_map[$module];
        $auth_class_instance = call_user_func([$auth_class,'instance']);
        $login_id = $auth_class_instance->id ? $auth_class_instance->id : 0;
        write_exe_log($use_time,$use_mem,$login_id);
        return true;
    }    
}
